﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Threading;
using System.Windows.Threading;
using Logistics.Utils;

namespace Logistics.Win
{
    
    /// <summary>
    /// Interaction logic for BusyWaitWin.xaml
    /// </summary>
    public partial class BusyWaitWin : Window
    {
        private IWorkObject WorkObject;

        private Thread WorkThread;

        public bool AutoStart = false;
        public bool AllowCancle = false;
        public bool AutoCloase = true;

        public BusyWaitWin(String processHeader,IWorkObject worker)
        {
            InitializeComponent();

            this.txtHeader.Text = processHeader;
            this.WorkObject = worker;
            this.WorkObject.Update += new UpdateEventHander(WorkObject_Update);
            this.WorkObject.Complete += new CompleteEventHander(WorkObject_Complete);
            this.WorkObject.Fail += new FailEventHander(WorkObject_Fail);
        }

        
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            initButton();
            if (AutoStart)
            {
                StartWork();
            }
        }
        private void BtnStart_Click(object sender, RoutedEventArgs e)
        {
            this.BtnStart.IsEnabled = false;
            StartWork();
        }

        private void BtnCancle_Click(object sender, RoutedEventArgs e)
        { 
            CancleWork();
            initButton();
        }
        private void initButton()
        {
            this.BtnStart.IsEnabled = !AutoStart;
            this.BtnCancle.IsEnabled = AllowCancle;
        }
        
        /// <summary>
        /// 启动异步工作
        /// </summary>
        private void StartWork()
        {
            ThreadStart threadStart = new ThreadStart(WorkObject.DoWork);
            this.WorkThread = new Thread(threadStart);
            this.WorkThread.IsBackground = true;
            this.WorkThread.Start();
        }
        /// <summary>
        /// 取消异步工作
        /// </summary>
        private void CancleWork()
        {
            this.DialogResult = false;
            if (this.WorkThread != null && this.WorkThread.ThreadState == ThreadState.Running)
            {
                this.WorkThread.Abort();
            }
            initButton();
        }

        /// <summary>
        /// 更新状态
        /// </summary>
        /// <param name="state"></param>
        private void WorkObject_Update(WorkState state)
        {
            Application.Current.Dispatcher.Invoke(new Action(() =>
            {
                if (state.Process>=0 && state.Process <= 100)
                {
                    this.progressBarx.Value = state.Process; 
                    this.txtMsg.Text = state.ProcessMsg;
                }
            }));
        }
        /// <summary>
        /// 工作完成
        /// </summary>
        private void WorkObject_Complete()
        {
            Application.Current.Dispatcher.Invoke(new Action(() =>
            {
                this.progressBarx.Value = 100;
                this.BtnStart.IsEnabled = !AutoStart;
                this.BtnCancle.IsEnabled = AllowCancle;

                if (AutoCloase)
                {
                    this.DialogResult = true;
                }
            }));
        }
        /// <summary>
        ///  工作失败
        /// </summary>
        /// <param name="state"></param>
        private void WorkObject_Fail(FailState state)
        {
            Application.Current.Dispatcher.Invoke(new Action(() =>
            {
                LogUtils.WriteError(state.FailedMsg);
                this.txtMsg.Text = state.FailedMsg;
            }));
        }
    }
}
